// #pragma once
// 定义PTP相关数据结构
#include <stdint.h>

// #include <atomic>
// #include <string>

// ptp帧的dmac地址
const uint8_t PTP_DMAC[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x0E};

// ptp帧的消息类型
enum PtpMsgType
{
        E_SYNC = 0x0,
        E_PDELAY_REQ = 0x2,
        E_PDELAY_RESP = 0x3,
        E_FOLLOW_UP = 0x8,
        E_PDELAY_RESP_FOLLOW_UP = 0xA,
        E_ANNOUNCE = 0xB,
        E_SIGNAL = 0xC,
};

enum PtpMsgLength
{
        PTP_SYNC_LENGTH = 44,
        PTP_PDELAY_REQ_LENGTH = 54,
        PTP_PDELAY_RESP_LENGTH = 54,
        PTP_PDELAY_RESP_FLLOW_UP_LENGTH = 54,
        PTP_SIGNAL_LENGTH = 60,
        PTP_FOLLOW_UP_LENGTH = 76,
        PTP_ANNOUNCE_LENGTH = 76,
};

// 固定值
#define PTP_MINOR_VERSION_PTP 0x0 // minorVersionPtp
#define PTP_VERSION_PTP 0x2       // versionPtp
#define PTP_MAJOR_SDO_ID 0x1
#define PTP_MINOR_SDO_ID 0x00  // MinorSdoId
#define PTP_ETH_TYPE 0x88F7    // ptp帧的一台类型
#define PTP_CONTROL_FIELD 0x00 // controlField

#define PTP_TLV_TYPE 0x3
#define PTP_LENGTH_FIELD 0x1C
#define PTP_ORGANIZATION_ID 0x80C2
#define PTP_ORGANIZATION_SUB_TYPE 0x1

// 以太头长度
#define ETH_HEADER_LEN 14

const uint16_t PTP_UTC_REASONABLE = 0x000c;
const uint16_t PTP_TIMESCALE = 0x0008;
// sync类型
enum PtpFlags
{
        ONE_STEP = 0x0000,
        TOW_STEP = 0x0200,
};

// ptp头
struct PtpHeader
{
        // 这里为了不转换网络序 转变地址
        uint8_t message_type : 4;
        uint8_t major_sdo_id : 4;
        uint8_t version_ptp : 4;
        uint8_t minor_version_ptp : 4;

        uint16_t message_length;
        uint8_t domain_number;
        uint8_t minor_sdo_id;
        uint16_t flags;
        uint64_t correction_field;
        uint32_t message_type_specific;
        // sourcePortIdentity 共10字节
        uint8_t clock_identify[8]; // 8字节
        uint16_t port_number;      // 2字节
        // sourcePortIdentity
        uint16_t sequence_id;
        uint8_t control_field;
        uint8_t log_message_interval;
} __attribute__((packed));

// Sync报文
struct PtpSync
{
        PtpHeader ptp_header;
        uint8_t padding[10];
} __attribute__((packed));

// follow_up报文
struct PtpFollowUp
{
        PtpHeader ptp_header;
        uint8_t precise_origin_timestamp[10];
        uint16_t tlv_type;
        uint16_t length_field;
        uint8_t orgainzation_id[3];
        uint8_t organization_sub_type[3];
        int32_t cumulative_scaled_rate_offset;
        uint16_t gm_time_base_indicator;
        uint8_t last_gm_phase_change[12];
        uint32_t scaled_last_gm_freq_change;

} __attribute__((packed));

// Pdelay_req报文
struct PtpPdelayReq
{
        PtpHeader ptp_header;
        uint8_t padding[20];
} __attribute__((packed));

// Pdelay_resp报文
struct PtpPdelayResp
{
        PtpHeader ptp_header;
        uint8_t request_receipt_timestamp[10]; // 时间类型为字符数组
        uint8_t clock_identify[8];
        uint16_t port_number;
} __attribute__((packed));

// Pdelay_resp_follow_up报文
struct PtpPdelayRespFollowUp
{
        PtpHeader ptp_header;
        uint8_t request_origin_timestamp[10];
        uint8_t clock_identify[8];
        uint16_t port_number;

} __attribute__((packed));

struct PtpPathTraceTLV
{
        uint16_t tlv_type;
        uint16_t length_field;
        uint8_t path_sequence[8];
};

// ptp announce
struct PtpAnnounce
{
        PtpHeader ptp_header;
        uint8_t reserved[10];
        uint16_t current_utc_offset;
        uint8_t reserved1;
        uint8_t gm_priority1;
        uint8_t gm_clock_class;
        uint8_t gm_clock_accuracy;
        uint16_t gm_clock_var;
        uint8_t gm_priority2;
        uint8_t gm_identify[8];
        uint16_t steps_removed;
        uint8_t time_source;
        PtpPathTraceTLV path_trace_tlv;
} __attribute__((packed));

// Pdelay_resp_follow_up报文
struct PtpSignal
{
        PtpHeader ptp_header;
        uint8_t target_port_identify[10];
        uint8_t req_tlv[16];
} __attribute__((packed));

// 链路延时测量相关头文件
enum Port_delay_state
{
        NONE = 0,
        SEND_REQ,            // 发送pdelay_req
        RECV_RESP,           // 收到Pdelay_resp
        RECV_RESP_FOLLOW_UP, // 收到Pdelay_resp_follow_up
        DONE                 // 完成链路延时测量
};

struct Port_delay
{
        uint16_t port_slave_id;     // 端口标识
        uint16_t sequence_id;       // 报文序列号
        uint64_t timestamp_1;       // 链路延时计算时间戳 t1
        uint64_t timestamp_2;       // 链路延时计算时间戳 t2
        uint64_t timestamp_3;       // 链路延时计算时间戳 t3
        uint64_t timestamp_4;       // 链路延时计算时间戳 t4
        uint64_t last_timestamp_3;  // 上一次链路延时计算时间戳 t3
        uint64_t last_timestamp_4;  // 上一次链路延时计算时间戳 t4
        uint64_t link_delay;        // 链路延时
        double neighbour_rateratio; // 邻居频比
};

struct Init_pack
{
        uint8_t padding[24]; // 正常情况24 测试是16
        PtpHeader ptp_header;
        uint8_t reserve[64]; // 空余64 以防越界
} __attribute__((packed));

// 初始化PTP头
int init(PtpHeader *ptpheader, PtpMsgType type);

// 发pdelay_req
uint8_t *sendPdelayReq();
// 发送Pdelay_resp
uint8_t *sendPdelayResp();
// 发送Pdelay_resp_follow_up
uint8_t *sendPdelayRespFollowUp();

// 接收和解析Pdelay_req
int parsePdelayReq(struct Pack_rec tsmp_pack);
// 收resp
int parsePdelayResp(struct Init_pack init_pack);
// 收resp_follow_up
int parsePdelayRespFollowUp(struct Init_pack init_pack);

// 10 字节转换为 8
uint64_t BufferToTimeStamp(const uint8_t *buffer);

// 计算链路延时
int calculateLinkDelay();
// 计算邻居频比
int calculateNR();

// 创建套接字
int create_socket_handle(const char *net_interface);
// 将本节点的GM频率比写进寄存器
int w_register(uint32_t addr);